package com.saer.client.tab.visita;

import java.util.List;

import com.google.gwt.user.client.Random;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.saer.client.SaerEntryPoint;
import com.saer.client.common.Utils;
import com.saer.client.constants.ClientConstants;
import com.saer.client.dto.diagnostico.VisitaDto;
import com.saer.client.dto.network.MsgDto;
import com.saer.client.grid.diagnostico.VisitaGrid;
import com.saer.client.layout.MainLayout;
import com.saer.client.tab.BaseTab;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.layout.VStack;

public class VisitaTab extends BaseTab {

	private int clientId = Random.nextInt();
	private VStack vstack = new VStack();
	private VisitaGrid visitaGrid = null;
	private Timer visitaReloadTimer = null;
	private MainLayout mainLayout = null;

	public VisitaTab(MainLayout vMainLayout) {
		super(ClientConstants.VISITA);
		
		this.setTitle("Asistencia Remota");

		this.mainLayout = vMainLayout;
		
		vstack.setSize("100%", "100%");
		vstack.setMembersMargin(10);
		vstack.setCanDropComponents(false);

		setPane(vstack);

		visitaReloadTimer = new Timer() {
			@Override
			public void run() {
				SaerEntryPoint.getRemotewebservice().getNewVisita(clientId,
						SaerEntryPoint.getLogin().getCentroMedicoType(),
						SaerEntryPoint.getLogin().getCentroMedicoId(),
						new NewVisitaCallBack());
			}
		};

		SaerEntryPoint.getRemotewebservice().getVisitas(
				SaerEntryPoint.getLogin().getCentroMedicoType(),
				SaerEntryPoint.getLogin().getCentroMedicoId(),
				new VisitaCallBack());

	}
	
	public void stopReload() {
		visitaReloadTimer.cancel();
		SaerEntryPoint.getRemotewebservice().endPoll(clientId,
				SaerEntryPoint.getLogin().getCentroMedicoType(),
				SaerEntryPoint.getLogin().getCentroMedicoId(),
				new NewVisitaCallBack());
	}

	private class VisitaCallBack implements AsyncCallback<List<VisitaDto>> {

		public void onFailure(Throwable arg0) {
			showErrorIcon(arg0.getMessage(), this.getClass().getName());
		}

		public void onSuccess(List<VisitaDto> result) {
			if (!validate(result)) {
				showErrorIcon("No hay visitas", this.getClass().getName());
			} else {
				// destroy old grid if exists
				if (visitaGrid != null) {
					vstack.removeMember(visitaGrid);
					visitaGrid.destroy();
				}

				// create new grid
				visitaGrid = new VisitaGrid(VisitaTab.this);
				vstack.addMember(visitaGrid);
				visitaGrid.setData(result);
			}

			visitaReloadTimer.cancel();
			visitaReloadTimer.schedule(1000);
		}

		private boolean validate(List<VisitaDto> result) {
			if (!Utils.isEmpty(result)) {
				return true;
			}
			return false;
		}
	}
	
	private class NewVisitaCallBack implements AsyncCallback<MsgDto> {

		public void onFailure(Throwable arg0) {
			showErrorIcon(arg0.getMessage(), this.getClass().getName());
		}

		public void onSuccess(MsgDto result) {
			if (validate(result)) {
				if (result.getAction() == ClientConstants.OP_NEW_VISITA) {
					SaerEntryPoint.getRemotewebservice().getVisitas(
							SaerEntryPoint.getLogin().getCentroMedicoType(),
							SaerEntryPoint.getLogin().getCentroMedicoId(),
							new VisitaCallBack());
				} else if (result.getAction() == ClientConstants.OP_POLL_AGAIN){
					visitaReloadTimer.cancel();
					visitaReloadTimer.schedule(1000);
				} else if (result.getAction() == ClientConstants.OP_POLL_ERROR) {
					showErrorIcon(result.getData(), this.getClass().getName());
				} else if (result.getAction() == ClientConstants.OP_POLL_END) {
					if (!Utils.isEmpty(result.getData())) {
						SC.warn(result.getData());
						SaerEntryPoint.loadLoginScreen();
					}
					return;
				} else {
					showErrorIcon("Operacion Desconocida", this.getClass().getName());
				}
			} else {
				showErrorIcon("Error desconocido (mensaje corrupto)", this.getClass().getName());
			}
		}

		private boolean validate(MsgDto result) {
			if (result != null && result.getClientId() == clientId) {
				return true;
			}
			return false;
		}
	}

	public MainLayout getMainLayout() {
		return mainLayout;
	}

	public VisitaGrid getVisitaGrid() {
		return visitaGrid;
	}
	
}
